package com.study.lihan.proxy.cglib;

import com.study.lihan.proxy.UserService;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author: lihan
 * @Date : 2021/12/31- 12
 * @DESCRIPTION: com.study.lihan.proxy
 * @version: 1.0
 */
public class TestCglib {
    public static void main(String[] args) {
        UserService target = new UserService();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(UserService.class);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                if(method.getName().equals("test")){
                    System.out.println("before.....");
                  Object result = method.invoke(target,objects);
                  // Object result = methodProxy.invoke(target,objects);
                  //Object result = methodProxy.invokeSuper(o,objects);
                    System.out.println("after.....");
                    return result;
                }
                return  method.invoke(target,objects);
            }
        });
        UserService userService = (UserService) enhancer.create();
        userService.test();

    }
}
